package com.smartdo.crawler.utils;

import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.List;
import java.util.Set;

/**
 * redis链接工厂
 */
public class RedisFactory {

    private static volatile JedisPool pool;

    private static String host = PropertiesUtil.getPropertie("redis_hosts");

    private static String password = PropertiesUtil.getPropertie("redis_password");

    public static JedisPool getInstance() {
        if (pool == null) {
            synchronized (RedisFactory.class) {
                if (pool == null) {
                    JedisPoolConfig config = new JedisPoolConfig();
                    config.setMaxTotal(1000);
                    config.setMaxIdle(50);
                    config.setMaxWaitMillis(10000);
                    config.setTestOnBorrow(true);
                    config.setTestOnReturn(true);
                    config.setTestWhileIdle(true);
                    config.setTimeBetweenEvictionRunsMillis(30000);
                    config.setNumTestsPerEvictionRun(10);
                    config.setMinEvictableIdleTimeMillis(60000);
                    if (StringUtils.isNotBlank(password)) {
                        pool = new JedisPool(config, host, 6379, 100000, password);
                    } else {
                        pool = new JedisPool(config, host, 6379, 100000);
                    }
                }
            }
        }
        return pool;
    }

    public synchronized static Jedis getJedis() {
        return getInstance().getResource();
    }

    public synchronized static Jedis getJedis(int index) {
        Jedis jedis = getJedis();
        jedis.select(index);
        return jedis;
    }

    /**
     * 将SET集合的数据复制到List集合中
     *
     * @param setKey
     * @param listKey
     */
    public static void copyAsinSetToList(String setKey, String listKey) {
        Jedis jedis = getJedis();
        Set<String> valueSet = jedis.smembers(setKey);
        valueSet.forEach(value -> {
            jedis.lpush(listKey, value);
        });
        getInstance().returnResource(jedis);
    }

    /**
     * 将SET集合的数据复制到List集合中
     *
     * @param key
     */
    public static void delKey(String key) {
        Jedis jedis = getJedis();
        jedis.del(key);
        getInstance().returnResource(jedis);
    }

    /**
     * 填充LIST集合
     *
     * @param key
     * @param list
     */
    public static void addList(String key, List<String> list) {
        Jedis jedis = getJedis();
        list.forEach(value -> {
            jedis.lpush(key, value);
        });
        getInstance().returnResource(jedis);
    }

    public static synchronized long getListSize(String key) {
        Jedis jedis = getJedis();
        long value = jedis.llen(key);
        getInstance().returnResource(jedis);
        return value;
    }

    public synchronized static String getListValue(String key) {
        Jedis jedis = getJedis();
        String value = jedis.lpop(key);
        getInstance().returnResource(jedis);
        return value;
    }


    public synchronized static void setListValue(String key, String value) {
        Jedis jedis = getJedis();
        jedis.rpush(key, value);
        getInstance().returnResource(jedis);
    }

    public synchronized static boolean sismember(String key, String value) {
        Jedis jedis = getJedis();
        boolean b = jedis.sismember(key, value);
        getInstance().returnResource(jedis);
        return b;
    }

    public synchronized static void setSetValue(String key, String value) {
        Jedis jedis = getJedis();
        jedis.sadd(key, value);
        getInstance().returnResource(jedis);
    }

    public synchronized static void setHasValue(String rkey, String key, String value) {
        Jedis jedis = getJedis();
        jedis.hset(rkey, key, value);
        getInstance().returnResource(jedis);
    }

    public synchronized static String getHashValue(String rkey, String key) {
        Jedis jedis = getJedis();
        String restr = jedis.hget(rkey, key);
        getInstance().returnResource(jedis);
        return restr;
    }

    public synchronized static void removeHashValue(String rkey, String key) {
        Jedis jedis = getJedis();
        jedis.hdel(rkey, key);
        getInstance().returnResource(jedis);
    }
}